package com.lanou.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.entity.ClassQaQuestion;
import com.lanou.service.ClassQaQuestionService;
import com.lanou.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @ClassName ClassQaQuestionController
 * @Description TODO
 * @Author cuiruitong
 * @Date 2025年08月14日 19:47
 * @Version 1.0
 */
@RestController
@RequestMapping("classQaQuestion")
public class ClassQaQuestionController {
    @Autowired
    private ClassQaQuestionService classQaQuestionService;
    //添加问题
    @PostMapping
    public R addClassQaQuestion(@RequestBody ClassQaQuestion classQaQuestion) {
        classQaQuestion.setTime(new Date());
        int i = classQaQuestionService.addClassQaQuestion(classQaQuestion);
        if (i>0){
            return R.ok();
        }else {
            return R.fail("添加失败");
        }
    }
    //根据课程id分页查询问题
    @GetMapping("getClassQaQuestionByCourseId")
    public R getClassQaQuestion(int courseId,@RequestParam(required = false) String orderRule,@RequestParam(required = false,defaultValue = "1")int pageNum,@RequestParam(required = false,defaultValue = "5") int pageSize) {
        PageInfo<ClassQaQuestion> classQaQuestionPageInfo = classQaQuestionService.selectQuestionsWithAnswers(courseId,orderRule,pageNum, pageSize);

        return R.ok(classQaQuestionPageInfo);
    }

    //根据问题id获取问题详情
    @GetMapping("{id}")
    public R getClassQaQuestionById(@PathVariable("id") int id) {
        ClassQaQuestion questionById = classQaQuestionService.getQuestionById(id);
        return R.ok(questionById);
    }

}
